<?php
namespace Home\Controller;
use Common\Controller\CommonController;

/**
 * 免费打牌
 * @author 张亚伟 <zhangyw@zhijin101.com>
 */
class FreePlayController extends CommonController
{

    public function index()
    {
        $model = M('free_play');
        $Count = $model->count();
        $Page = new \Think\Pager($Count, 10);
        $show = $Page->showhtml();
        $keyword = I('keyword');
        $where = ['f.check_status'=>['in', [1]]];
        if(!empty($keyword)){
            $where['f.card_room|f.mobile'] = array('like',"%".$keyword."%");
        }

        if ($area = I('area')) {
            $where['s.pro'] = explode('/', $area)['0'];
            $where['s.city'] = explode('/', $area)['1'];
            $where['s.area'] = explode('/', $area)['2'];
            $this->assign('areaArr',$area);
        }

        $list = $model->alias('f')
                ->join('left join __SHOP__ s on  f.shop_id=s.id')
                ->where($where)
                ->limit($Page->firstRow.','.$Page->listRows)
                ->order('f.id desc')
                ->field('f.*,s.pro,s.city,s.area')
                ->select();
        if($list){
            foreach($list as $key=>&$row){
                $row['status'] = $row['status'] ? '开启' : '关闭';
            }
        }
        $this->assign('page',$show);
        $this->assign('list', $list);
        $this->display();        
    }

    public function add()
    {
        if(IS_POST){
            $data = I('post.');
            if(empty($data['card_room'])){
                $this->ajaxReturn(['s'=>false, 'm'=>'请输入棋牌室名称']);
            }else{
                if(empty($data['mobile'])){
                    $this->ajaxReturn(['s'=>false, 'm'=>'请输入电话']);
                }
                $shop_info = M('shop')->where(['shopname'=>$data['card_room'], 'tel'=>$data['mobile'], 'status'=>1])->find();
                if(empty($shop_info)){
                    $this->ajaxReturn(['s'=>false, 'm'=>'棋牌室还没有入驻']);
                }else{
                    $data['shop_id'] = $shop_info['id'];
                    if($shop_info['uid']){
                        $data['uid'] = $shop_info['uid'];
                    }
                }
            }
            if(empty($data['applicant_name'])){
                $this->ajaxReturn(['s'=>false, 'm'=>'请输入申请人姓名']);
            }

            // if(empty($data['address'])){
            //     $this->ajaxReturn(['s'=>false, 'm'=>'请输入地址']);
            // } 
            // if(empty($data['id_card'])){
            //     $this->ajaxReturn(['s'=>false, 'm'=>'请输入身份证号']);
            // }
            if(empty($data['free_date'])){
                $this->ajaxReturn(['s'=>false, 'm'=>'请输入免费日期']);
            }
            $data['check_status'] = 1;
            // $data['business_license'] = !empty($data['business_license']) ? implode(',', $data['business_license']) : '';
            $data['free_date'] = $data['free_date'] ? strtotime($data['free_date']) : 0;
            $model = M('free_play');
            $id = $model->add($data);
            if($id){
                $this->ajaxReturn(array('s'=>true,'m'=>'添加成功', 'url'=>U('index')));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'添加失败!'));
            }
        }else{
            $this->display();
        }
    }

    public function edit()
    {
        $id = I('id');
        if(IS_POST){
            $data = I('post.');
            if(empty($data['card_room'])){
                $this->ajaxReturn(['s'=>false, 'm'=>'请输入棋牌室名称']);
            }
            if(empty($data['applicant_name'])){
                $this->ajaxReturn(['s'=>false, 'm'=>'请输入申请人姓名']);
            }
            if(empty($data['mobile'])){
                $this->ajaxReturn(['s'=>false, 'm'=>'请输入电话']);
            } 
            // if(empty($data['address'])){
            //     $this->ajaxReturn(['s'=>false, 'm'=>'请输入地址']);
            // } 
            // if(empty($data['id_card'])){
            //     $this->ajaxReturn(['s'=>false, 'm'=>'请输入身份证号']);
            // }
            if(empty($data['free_date'])){
                $this->ajaxReturn(['s'=>false, 'm'=>'请输入免费日期']);
            }
            // $data['business_license'] = !empty($data['business_license']) ? implode(',', $data['business_license']) : '';
            $data['free_date'] = $data['free_date'] ? strtotime($data['free_date']) : 0;
            $model = M('free_play');

            $res = $model->where(['id'=>$id])->save($data);
            if($res!==false){
                $this->ajaxReturn(array('s'=>true,'m'=>'修改成功', 'url'=>U('index')));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'修改失败!'));
            }
        }else{
            $info = M('free_play')->where(['id'=>$id])->find();
            $this->assign('info', $info);
            $this->display();
        }
    }

    public function apply(){
        $model = M('free_play');
        $Count = $model->count();
        $Page = new \Think\Pager($Count, 10);
        $show = $Page->showhtml();
        $keyword = I('keyword');
        $where = ['f.check_status'=>['in', [0]]];
        if(!empty($keyword)){
            $where['f.card_room|f.mobile'] = array('like',"%".$keyword."%");
        }

        if ($area = I('area')) {
            $where['s.pro'] = explode('/', $area)['0'];
            $where['s.city'] = explode('/', $area)['1'];
            $where['s.area'] = explode('/', $area)['2'];
            $this->assign('areaArr',$area);
        }

        $list = $model->alias('f')
                ->join('left join __SHOP__ s on  f.shop_id=s.id')
                ->where($where)
                ->limit($Page->firstRow.','.$Page->listRows)
                ->order('f.id desc')
                ->field('f.*,s.pro,s.city,s.area')
                ->select();
        if($list){
            $status_arr = ['申请中', '同意', '拒绝'];
            foreach($list as $key=>&$row){
                $row['status'] = $status_arr[$row['check_status']];
            }
        }
        $this->assign('page',$show);
        $this->assign('list', $list);
        $this->display();
    }

    public function apply_edit(){
        $id = I('id');
        if(IS_POST){
            $data = I('post.');
            if(empty($data['card_room'])){
                $this->ajaxReturn(['s'=>false, 'm'=>'请输入棋牌室名称']);
            }
            if(empty($data['applicant_name'])){
                $this->ajaxReturn(['s'=>false, 'm'=>'请输入申请人姓名']);
            }
            if(empty($data['mobile'])){
                $this->ajaxReturn(['s'=>false, 'm'=>'请输入电话']);
            } 
            // if(empty($data['address'])){
            //     $this->ajaxReturn(['s'=>false, 'm'=>'请输入地址']);
            // } 
            // if(empty($data['id_card'])){
            //     $this->ajaxReturn(['s'=>false, 'm'=>'请输入身份证号']);
            // }
            if(empty($data['free_date'])){
                $this->ajaxReturn(['s'=>false, 'm'=>'请输入免费日期']);
            }
            // $data['business_license'] = !empty($data['business_license']) ? implode(',', $data['business_license']) : '';
            $data['free_date'] = $data['free_date'] ? strtotime($data['free_date']) : 0;
            if($data['check_status']){
                $data['check_time'] = time();
                if($data['check_status'] == 1){
                    $data['status'] = 1;
                }
            }
            $model = M('free_play');

            $res = $model->where(['id'=>$id])->save($data);
            if($res!==false){
                $this->ajaxReturn(array('s'=>true,'m'=>'修改成功', 'url'=>U('apply')));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'修改失败!'));
            }
        }else{
            $info = M('free_play')->where(['id'=>$id])->find();
            $this->assign('info', $info);
            $this->display();
        }
    }

    public function del(){
        $id = I('id');
        $res = M('free_play')->where(['id'=>$id])->delete();
        if($res){
            $this->ajaxReturn(array('s'=>true,'m'=>'删除成功', 'url'=>U('index')));
        }else{
            $this->ajaxReturn(array('s'=>false,'m'=>'删除失败!'));
        }
    }
}
